﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace System
{
    public static class ArrayExtensions
    {
        /// <summary>
        /// 拼接数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="att"></param>
        /// <returns></returns>
        public static T[] Combine<T>(this T[] source, T[] att)
        {
            return ArrayHelper.Combine(source, att);
        }


        public static void Each<T>(this IEnumerable<T> enu, Action<int, T> action)
        {
            if (enu == null)
            {
                return;
            }
            int index = 0;
            foreach (var item in enu)
            {
                action?.Invoke(index, item);
                index++;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enu"></param>
        /// <param name="action">该函数反回true则终止迭代</param>
        public static void Each<T>(this IEnumerable<T> enu, Func<int, T, bool> action)
        {
            if (enu == null || action == null)
            {
                return;
            }
            int index = 0;
            foreach (var item in enu)
            {
                var bk = action.Invoke(index, item);
                if (bk)
                {
                    break;
                }
                index++;
            }
        }

        public static IEnumerable<T> GetEnumerator<T>(this IEnumerable<T> enu, int startIndex, int count = int.MaxValue)
        {
            if (enu == null)
            {
                yield break;
            }
            int index = 0;
            int c = 0;
            foreach (var item in enu)
            {
                if (index >= startIndex && c <= count)
                {
                    c++;
                    yield return item;
                }
                else
                {
                    yield break;
                }

                index++;
            }
        }

        public static int IndexOf(this IEnumerable enu, object ele)
        {
            int index = 0;
            foreach (var item in enu)
            {
                if (item.Equals(ele))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
    }
}
